(function($) {
	
	var myview = $.extend({},$.fn.datagrid.defaults.view,{
	    onAfterRender:function(target){
	        $.fn.datagrid.defaults.view.onAfterRender.call(this,target);
	        var opts = $(target).datagrid('options');
	        var vc = $(target).datagrid('getPanel').children('div.datagrid-view');
	        vc.children('div.datagrid-empty').remove();
	        if (!$(target).datagrid('getRows').length){
	            var d = $('<div class="datagrid-empty"></div>').html(opts.emptyMsg || '对不起，没有内容！').appendTo(vc);
	            d.css({
	                position:'absolute',
	                left:0,
	                top:50,
	                width:'100%',
	                textAlign:'center'
	            });
	        }
	}});
	
	$.addIssDatagrid = function (t, p) {
		//if (t.grid) return false; //return if already exist
		p = $.extend({
			title: '查询结果',
			width: 'auto',
			height: 420,
			nowrap: true,
			formid: '',
			autoRowHeight: false,
			striped: true,
			url : '',
			sortName: '',
			sortOrder: 'asc',
			remoteSort: false,
			idField: '',
			pagination: true,
			singleSelect: true,
			showFooter: true,
			frozenColumns: '',
			columns: '',
			toolbar: '',
			rownumbers:'true',
			queryParams: {},
			pageList:[10,20,50,100,500,1000,2000],
			onLoadSuccess: function(data){},
			onCheck: function(i,row){},
			onUncheck: function(i,row){},
			onCheckAll: function(i,row){},
			onUncheckAll: function(i,row){},
			rowStyler: function(){},
			onSelectAll:function(row){},
			onUnselectAll:function(row){}
		}, p);
		
		if(p.width == 'auto'){
			p.width = document.documentElement.clientWidth - 50;
		}
		if(p.width == 'gridauto')
		{
			p.width = 'auto';
		}
	
	  $(t).datagrid({
			title: p.title,
			width: p.width,
			height: p.height,
			nowrap: p.nowrap,
			formid: p.formid,
			view: myview,
			autoRowHeight: false,
			striped: true,
			url : p.url,
			sortName: p.sortName,
			sortOrder: p.sortOrder,
			remoteSort: p.remoteSort,
			idField: p.idField,
			pagination: p.pagination,
			singleSelect: p.singleSelect,
			rownumbers: p.rownumbers,
			showFooter: p.showFooter,
			frozenColumns: p.frozenColumns,
			columns: p.columns,
			toolbar: p.toolbar,
			queryParams: p.queryParams,
			pageList:p.pageList,
			onLoadSuccess: function(data){
				//显示AJAX请求后的提示信息
				$.fn.ajaxmessage(data);
				/* add by xiakai  没有分页的时候会报js错
				var total = $(t).datagrid('getPager').pagination("options").total;
				var pageSize = $('#datagrid').datagrid('options')['pageSize'];

				if(total > 0) {
					$('#datagrid').datagrid('resize', {
						 height : pageSize*39
					});
				}*/
				
				//用户自定义
				p.onLoadSuccess(data);
			},
			onLoadError:function(){},
			onCheck:function(i,row){p.onCheck(i,row)},
			onUncheck: function(i,row){p.onUncheck(i,row)},
			onCheckAll: function(i,row){p.onCheckAll(i,row)},
			onUncheckAll: function(i,row){p.onUncheckAll(i,row)},
			onSelectAll:function(row){p.onSelectAll(row);},
			onUnselectAll:function(row){
				p.onUnselectAll(row);
			},
			rowStyler: p.rowStyler
	  });
	};
	
	$.fn.issdatagrid = function (p) {
			$.addIssDatagrid($(this), p);
	};
	
})(jQuery);